package com.rocksdata.mc.business;

import com.rocksdata.mc.BuildConfig;
import com.rocksdata.mc.entity.NewsPageInfo;
import com.rocksdata.mc.entity.NewsTagInfo;
import com.rocksdata.mc.http.HttpClient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 陈堂正 on 2017/10/23.
 */

public class NewsManager implements BusinessInterface.INewsManager{
    private static final String URL_GET_NEWS_TAGS = BuildConfig.HOST + "/news/getNewsProgram";
    private static final String URL_GET_NEWS_LIST = BuildConfig.HOST + "/news/getNewsByProgramID";

    private static BusinessInterface.INewsManager instance;

    private NewsManager(){
    }

    static synchronized BusinessInterface.INewsManager getInstance(){
        if(instance == null)
            instance = new NewsManager();
        return instance;
    }

    @Override
    public void getNewsTags(final BusinessInterface.OnBusinessResultListener listener){
        HttpClient.get(URL_GET_NEWS_TAGS, null, NewsTagInfo.class, new HttpClient.OnHttpResultListener<NewsTagInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<NewsTagInfo> data, String msg) {
                if(isSuccess){
                    listener.onBusinessResult(true, data, msg);
                }else{
                    listener.onBusinessResult(false, null, msg);
                }
            }
        });
    }

    @Override
    public void getNewsListByTag(String tagId, int pageIndex, final BusinessInterface.OnBusinessResultListener listener){
        Map<String, Object> map = new HashMap<>();
        if(tagId != null) {
            map.put("programCode", tagId);
            map.put("page", pageIndex);
        }
        HttpClient.get(URL_GET_NEWS_LIST, map, NewsPageInfo.class, new HttpClient.OnHttpResultListener<NewsPageInfo>(){

            @Override
            public void onResult(boolean isSuccess, List<NewsPageInfo> data, String msg) {
                if(isSuccess){
                    listener.onBusinessResult(true, data.get(0).getDataList(), msg);
                }else{
                    listener.onBusinessResult(false, null, msg);
                }
            }
        });
    }
}
